﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

using _XNA__RunningDeath.DTO;
using _XNA__RunningDeath.DTO.VatThe;
using _XNA__RunningDeath.GLO;


namespace _XNA__RunningDeath.BUS.VatThe
{
    class CBeMatDiChuyen_BUS
    {
        CVatThe_DTO _bematdichuyen; //Tạo lớp thông tin bề mặt di chuyển
        
        public Vector2 _posStand1; //vị trí  1
        public Vector2 _posStand2; //vị trí  2
        public Vector2 _posStand3; //vị trí  3

        double time = 0;

        public Rectangle _recHo;
        bool _IsNotExistPit = true;
        /// <summary>
        /// khỏi tạo các thông số 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="gameTime"></param>
        /// <param name="spriteBatch"></param>
        public CBeMatDiChuyen_BUS(ContentManager content, GameTime gameTime, SpriteBatch spriteBatch)
        {
            _bematdichuyen = new CBeMatDiChuyen_DTO(content, gameTime, spriteBatch);

            if (CGLOBAL.NewGame == enumGame.mLoad)
            {
                _posStand1 = CGLOBAL.pointBeMat01;
                _posStand2 = CGLOBAL.pointBeMat02;
                _posStand3 = CGLOBAL.pointBeMat03;
                _recHo = CGLOBAL._recHo;
            }
            else
            {
                _posStand1 = _bematdichuyen.Pointstart;

                int x1 = +_bematdichuyen.Dstoado["bagough1"].Width + CThamSo.doLechFrame;
                _posStand2 = new Vector2(_posStand1.X + x1, _bematdichuyen.Pointstart.Y);//

                int x2 = +_bematdichuyen.Dstoado["bagough2"].Width + CThamSo.doLechFrame;
                _posStand3 = new Vector2(_posStand2.X + x2, _bematdichuyen.Pointstart.Y);//

                _recHo = new Rectangle(-CGLOBAL.khoangCachHo, (int)_bematdichuyen.Pointstart.Y, CGLOBAL.khoangCachHo, 20);
            }
        }
        /// <summary>
        /// Update dùng để cập nhật 
        /// </summary>
        /// <param name="gameTime"></param>
        public void UpDate( GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime.TotalMilliseconds;

            if (time >= 1000)
            {
                if (_IsNotExistPit)
                {
                    createPit();
                    NoStackUp();
                    time = 0;
                }
            }
            if(_recHo.X+ _recHo.Width <0)
                _IsNotExistPit = true;

            float x1 = _posStand1.X;
            float x2 = _posStand2.X;
            float x3 = _posStand3.X;

            if (x1 + _bematdichuyen.Dstoado["bagough1"].Width <= 0)
            {
                x1 = _posStand3.X + _bematdichuyen.Dstoado["bagough3"].Width + CThamSo.doLechFrame;
            }
            else if (x2 + _bematdichuyen.Dstoado["bagough2"].Width <= 0)
            {
                x2 = _posStand1.X + _bematdichuyen.Dstoado["bagough1"].Width + CThamSo.doLechFrame;
            }
            else if (x3 + _bematdichuyen.Dstoado["bagough3"].Width <= 0)
            {
                    x3 = _posStand2.X + _bematdichuyen.Dstoado["bagough2"].Width + CThamSo.doLechFrame;
            }

            _posStand1 = new Vector2(x1 - CGLOBAL.speedBeMatDiChuyen, _posStand1.Y);
            _posStand2 = new Vector2(x2 - CGLOBAL.speedBeMatDiChuyen, _posStand2.Y);
            _posStand3 = new Vector2(x3 - CGLOBAL.speedBeMatDiChuyen, _posStand3.Y);
            _recHo = new Rectangle(_recHo.X - CGLOBAL.speedBeMatDiChuyen, _recHo.Y, CGLOBAL.khoangCachHo, 150);

            
        }
        /// <summary>
        /// 
        /// </summary>
        private void NoStackUp()
        {
            float x1 = _posStand1.X;
            float x2 = _posStand2.X;
            float x3 = _posStand3.X;

            if(x1 <= x2 && x2 < x1 + _bematdichuyen.Dstoado["bagough1"].Width )
            {
                x2 = x1 + _bematdichuyen.Dstoado["bagough1"].Width + CThamSo.doLechFrame;
            }

            if (x2 <= x3 && x3 < x2 + _bematdichuyen.Dstoado["bagough2"].Width)
            {
                x3 = x2 + _bematdichuyen.Dstoado["bagough2"].Width + CThamSo.doLechFrame;
            }

            if (x3 <= x1 && x1 < x3 + _bematdichuyen.Dstoado["bagough3"].Width)
            {
                x1 = x3 + _bematdichuyen.Dstoado["bagough3"].Width + CThamSo.doLechFrame;
            }

            _posStand1.X = x1;
            _posStand2.X = x2;
            _posStand3.X = x3;
        }
        /// <summary>
        /// 
        /// </summary>
        private void createPit()
        {
            float x1 = _posStand1.X;
            float x2 = _posStand2.X;
            float x3 = _posStand3.X;

            if (x1 > CThamSo.widthForm)
            {
                _recHo.X = (int)x3 + _bematdichuyen.Dstoado["bagough3"].Width + CThamSo.doLechFrame;
                x1 = _recHo.X + _recHo.Width + CThamSo.doLechFrame;
            }
            else if (x2 > CThamSo.widthForm)
            {
                _recHo.X = (int)x1 + _bematdichuyen.Dstoado["bagough1"].Width + CThamSo.doLechFrame;
                x2 = _recHo.X + _recHo.Width + CThamSo.doLechFrame;
            }
            else if (x3 > CThamSo.widthForm)
            {
                _recHo.X = (int)x2 + _bematdichuyen.Dstoado["bagough2"].Width + CThamSo.doLechFrame;
                x3 = _recHo.X + _recHo.Width + CThamSo.doLechFrame;
            }

            _posStand1.X = x1;
            _posStand2.X = x2;
            _posStand3.X = x3;
            _IsNotExistPit = false;
        }
        /// <summary>
        /// dùng để vẽ đối tượng ra
        /// </summary>
        /// <param name="gameTime"></param>
        public void Draw(GameTime gameTime)
        {
            _bematdichuyen.SpriteBatch.Draw(_bematdichuyen.VatThe1, _posStand1, Color.White);
            _bematdichuyen.SpriteBatch.Draw(_bematdichuyen.VatThe2, _posStand2, Color.White);
            _bematdichuyen.SpriteBatch.Draw(_bematdichuyen.VatThe3, _posStand3, Color.White);
        }
    }
}
